package com.push.notifications;

import java.util.ArrayList;
import java.util.List;


import android.app.TabActivity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;

import android.view.GestureDetector;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;

import android.widget.HorizontalScrollView;
import android.widget.TabHost;
import android.widget.TabHost.OnTabChangeListener;
import android.widget.Toast;
import android.widget.TabHost.TabSpec;

import android.util.Log;

import android.view.GestureDetector.SimpleOnGestureListener;
import android.view.LayoutInflater.Filter;
import android.view.View.OnClickListener;

import android.widget.TextView;


public class newsTabManager extends TabActivity {
	
	
	public static categoryItem currentItem = null;
	UI_ListHistory adapter;
	public static List<categoryItem> listSubNews = new ArrayList<categoryItem>();	
	
	
	public static TabHost mTabHost;
	HorizontalScrollView hScrollView;
	Intent intent;

	private static final int SWIPE_MIN_DISTANCE = 120;
	private static final int SWIPE_MAX_OFF_PATH = 250;
	private static final int SWIPE_THRESHOLD_VELOCITY = 200;
	private GestureDetector gestureDetector;
	View.OnTouchListener gestureListener;
	public static Handler han_newTabmanager;
	
	
	@Override
	public void onCreate(Bundle saveInstanceState){
		super.onCreate(saveInstanceState);
			        
	        setContentView(R.layout.tab_manager);
	        han_newTabmanager = new Handler() {

	            public void handleMessage(Message msg) {
	                super.handleMessage(msg);

	                switch(msg.what) {

	                case 0:
	                	finish();
	                    break;

	                }
	            }

	        };
	        mTabHost = (TabHost) findViewById(android.R.id.tabhost);
			mTabHost.getTabWidget().setDividerDrawable(null);
			
			TextView textTile = (TextView) findViewById(R.id.textVieggg0);
			if(textTile != null && currentItem != null)
				textTile.setText(currentItem.getName());
			
			Intent intent; // Reusable Intent for each tab

			listSubNews = databaseWrapper.getListSubNews(this,currentItem.getCategory());
			
			int lastestCategory = databaseWrapper.getLastestNews(this);
		
			int nCount = listSubNews.size();
			int nCurrentTab = 0;
			for(int i =0; i< nCount; i++)
			{
				categoryItem item = listSubNews.get(i);
				if(item != null)
				{
					if(item.getCategory() == lastestCategory)
						nCurrentTab = i;
					historyNewsCategory.curItem = item;
					intent = new Intent().setClass(getApplicationContext(), historyNewsCategory.class);
					setupTab(new TextView(this), item.getName(), intent);					
				}
			}
			mTabHost.setOnTabChangedListener(new TabChangeListener());
			mTabHost.setCurrentTab(nCurrentTab);
			hScrollView = (HorizontalScrollView)findViewById(R.id.scroller);
    		
    		int nMoveX = 0;
    		for(int j =0 ; j< nCurrentTab; j++)
    		{
    			nMoveX += mTabHost.getTabWidget().getChildTabViewAt(j).getWidth();
    		}
    	    hScrollView.scrollTo(nMoveX, 0);     
	    } 
	private void setupTab(final View view, final String tag, final Intent myIntent) {

		View tabview = createTabView(mTabHost.getContext(), tag);
		TabSpec setContent =  mTabHost.newTabSpec(tag).setIndicator(tabview).setContent(myIntent);
		mTabHost.addTab(setContent);
		//mTabHost.getTabWidget().getChildAt(0).getLayoutParams().height =30;
	}

	private static View createTabView(final Context context, final String text) {
		View view = LayoutInflater.from(context).inflate(R.layout.tabs_bg, null);
		TextView tv = (TextView) view.findViewById(R.id.tabsText);
		tv.setText(text);
		return view;
	}
	
	
	 public class TabChangeListener implements OnTabChangeListener
	  {
	    	@Override
	    	public void onTabChanged(String str){
	    		int i = mTabHost.getCurrentTab();
	    		hScrollView = (HorizontalScrollView)findViewById(R.id.scroller);
	    		
	    		int nMoveX = 0;
	    		for(int j =0 ; j< i; j++)
	    		{
	    			nMoveX += mTabHost.getTabWidget().getChildTabViewAt(j).getWidth();
	    		}
	    	    hScrollView.scrollTo(nMoveX, 0);
	    		Log.e("TabChangeListener", "TabChangeListener: ------Change list 123------------ History=" );

	    		historyNewsCategory.curItem = listSubNews.get(i);
	    	}
	  }	
		@Override
		protected void 	onUserLeaveHint(){
				  GCMIntentService.AppInterrupt =true;
			super.onUserLeaveHint();
		}
		@Override
		protected void onStop() {
			Log.e("newsTabManager", "onStop: news tab manager"+GCMIntentService.AppInterrupt);
			super.onStop();
		}
		@Override
		public void onWindowFocusChanged(boolean hasFocus) {
			  if (hasFocus)
			  {
				  GCMIntentService.AppInterrupt =false;
			  }
			// TODO Auto-generated method stub
			super.onWindowFocusChanged(hasFocus);
		}
	 
}
	